<template>
  <page-main class="page-doctor-list">
    <div class="p-x-15">
      <div class="p-t-15"></div>
      <!-- <div class="date-list">
        <div class="item" v-for="(item,index) in date.day" :key="index">
          <div>{{ item.week }}</div>
          <van-button size="mini" round block :class="date.active == index ? 'day active' : 'day'" @click="selectDay(item,index)">{{item.day}}</van-button>
        </div>
      </div> -->
      <div class="date-swipe">
        <van-swipe :show-indicators="false" :loop="false">
          <van-swipe-item v-for="column in 4" :key="column" class="date-list">
            <div class="item" v-for="(item,index) in date.day.slice((column-1) * 7, column*7)" :key="index">
              <div>{{ item.week }}</div>
              <van-button size="mini" round block :class="date.active == index + (column-1) *7 ? 'day active' : 'day'" @click="selectDay(item,index,column)">{{item.day}}</van-button>
            </div>
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="hd">
        <h3>坐诊医生<span v-if="date.day.length > 0">({{date.day[date.active].value}})</span></h3>
      </div>
      <van-empty description="该日期暂无排班医生" v-if="list.length == 0" />
      <div class="list fly-card" v-else>
        <div class="card-block" v-for="(item,index) in list" :key="index" @click="gotoNext(item)">
          <div class="item">
            <div class="thumb">
              <van-image :src="$util.baseUrl + item.staffPicture" round width="40px" height="40px" />
            </div>
            <div class="content">
              <div class="title">{{item.staffName}}</div>
              <div class="desc">{{$util.setName(item.professionalTitle, init_data.professional_list)}}</div>
            </div>
          </div>
          <div class="ft align-right">
            <span class="c-red">￥{{getFee(item.professionalTitle)}}</span><span class="ft-12 c-red">起</span>
            <!-- <span >{{item.schedulingType == '0' ? '(早上)' : '(下午)'}}</span> -->
            <van-tag type="primary" size="medium" round>剩余{{item.makeNumber}}</van-tag>
          </div>
        
        </div>
      </div>
    </div>
  </page-main>
</template>

<script>
import moment from 'moment';
export default {
  data(){
    return{
      pageParams:{},
      schedulingTime: null,
      date:{
        active: 0,
        index: 1,
        day:[]
      },
      list:[],
      init_data:{
        fee1_list: [],
        fee2_list: [],
        fee3_list: [],
        fee4_list: [],
      },
    }
  },
  async created(){
    this.pageParams = this.$route.query;
    this.schedulingTime = moment().format('YYYY-MM-DD')
    await this.getInitData();
    this.getDays();
    this.getDoctorList(this.schedulingTime);
  },
  methods:{
    async getInitData(){
      this.init_data.professional_list = await this.$util.getDictList('professional_title');
      this.init_data.staffLabel_list = await this.$util.getDictList('staff_label');
      this.init_data.professional_list.forEach(async item => {
        this.init_data['fee' + item.id + '_list'] = await this.$http('get', '/admin/registrationApi/GetRegistrationAndFee', {professionalTitleId: item.id});
      });      
    },
    getFee(id){
      let arr = this.init_data['fee' + id + '_list'].map(item => item.fee);
      let min = Math.min(...arr);
      return min;
    },
    getDoctorList(time){
      this.schedulingTime = time;
      let params = {
        officeId: this.pageParams.officeId,
        schedulingTime: time,
      };
      this.$http('get', '/admin/officeApi/officeStaff',params, (data,msg) => {
        let ids = [];
        data.forEach(item => {
          if(ids.indexOf(item.staffId) == -1){
            ids.push(item.staffId)
          }
        });
        let arr = [];
        ids.forEach(item => {
          let list = data.filter(sub => sub.staffId == item);
          let obj = {...list[0]};
          obj.makeNumber = 0;
          list.forEach(item => {
            if(time == moment().format('YYYY-MM-DD')){ // 判断是否是今天，再判断是上午，下午，晚上，然后根据当前的时间，去掉已过期的可预约数量
              let datetimes = new Date().getTime();
              let type0 = new Date(time + ' 12:00:00').getTime();
              let type1 = new Date(time + ' 18:00:00').getTime();
              let type2 = new Date(time + ' 22:30:00').getTime();
              if(item.schedulingType == '0'){
                if(datetimes < type0){
                  obj.makeNumber += Number(item.makeNumber);
                }
              }else if(item.schedulingType == '1'){
                if(datetimes < type1){
                  obj.makeNumber += Number(item.makeNumber);
                }
              }else if(item.schedulingType == '2'){
                if(datetimes < type2){
                  obj.makeNumber += Number(item.makeNumber);
                }
              }
            }else{
              obj.makeNumber += Number(item.makeNumber);
            }
            
          });
          arr.push(obj);
        })
        this.list = arr;
      })
    },
    getDays(){
      let arr = [];
      for(let i = 0; i < 28; i++){
        let day = moment().add(i,'days').format('YYYY-MM-DD');
        arr.push(day);
      }
      this.date.day = arr.map(item => {
        return {
          week: this.getWeek(item),
          day: Number(item.substring(8,10)),
          value: item
        }
      });
    },
    getWeek (date) {
      let week = moment(date).day()
      switch (week) {
        case 1:
          return '周一'
        case 2:
          return '周二'
        case 3:
          return '周三'
        case 4:
          return '周四'
        case 5:
          return '周五'
        case 6:
          return '周六'
        case 0:
          return '周日'
      }
    },
    selectDay(item,i,column){
      this.date.active = i + (column-1) * 7;
      this.getDoctorList(item.value);
    },
    gotoNext(item){
      let params = Object.assign(this.$route.query, {makeStaffId: item.staffId, makeStaffName: item.staffName, schedulingYear: this.schedulingTime});
      this.$util.gotoPage('/doctor/index', params);
    }
  }
}
</script>

<style>

</style>